<?php
require_once dirname(dirname(__FILE__)) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'check_user_login.php';
?>
<!DOCTYPE html>
<html>
    <head id="Head1">
        <title>
            Manage Category's Journals - Tesdo Admin
        </title>
        <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'head.php';
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'header.php';
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'script.php';
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'ckeditor.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_DAO . 'dao_journal.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_DAO . 'dao_category.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_DAO . 'dao_category_journal.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_pager.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_upload.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_data_input.php';
        ?>
        <script type="text/javascript">
            function call() {
                document.getElementById('hiddenMsg').value = editor.getData();
            }
            function my_confirm(delete_cat_journal_id) {
                if (confirm('Are you sure?')) {
                    set_value('delete_cat_journal_id', delete_cat_journal_id);
                    set_value('a_type', 'delete');
                    submit_form('form_delete');
                } else {
                    return false;
                }
            }
        </script>
    </head>
    <body>
        <?php
        //CLEAR & CANCEL
        if (!empty($_POST['btnClear']) || !empty($_POST['btnCancel'])) {
            ?>
            <script type="text/javascript">
                window.location = <?php echo LINK_ROOT . DIR_ADMIN . 'category_journal.php'; ?>;
            </script>
            <?php
        }

        //Get Objects
        $journal_access = new dao_journal();
        $category_access = new dao_category();
        $dao_category_journal = new dao_category_journal();
        $lib_pager = new lib_pager();

        //Check type and set $type value
        $type = 'insert';
        if (!empty($_POST['a_type'])) {
            $type = $_POST['a_type'];
        }
        if (isset($_GET['t'])) {
            if ($_GET['t'] == 'e')
                $type = 'edit';
        }

        //Get ID
        $delete_cat_journal_id = 0;
        if (!empty($_GET['delete_id'])) {
            $delete_cat_journal_id = $_GET['delete_id'];
        }
        $cat_journal_id = 0;
        if (!empty($_GET['cj_id'])) {
            $cat_journal_id = $_GET['cj_id'];
        }

        //Insert
        if (isset($_POST['btnSave']) && $_POST['btnSave'] == 'Insert') {
            if ((!isset($_POST['ddlJournals']) ||
                    !isset($_POST['ddlCategory']) ||
                    !isset($_POST['txtDescription']) ||
                    $_POST['ddlJournals'] == '' ||
                    $_POST['ddlCategory'] == '' ||
                    $_POST['txtDescription'] == '')) {
                ?>
                <script type="text/javascript">
                    alert('You need to fulfill the information!');
                </script>
                <?php
            } else {
//                $cat_journal_id = $_POST['cat_journal_id'];
                $description = $_POST['txtDescription'];
                $journal_id = $_POST['ddlJournals'];
                $category_id = $_POST['ddlCategory'];

                if ($type != "edit" && $dao_category_journal->check_exist($journal_id, $category_id)) {
                    ?>
                    <script type="text/javascript">
                        alert('<?php echo 'Relation existed, insert failed'; ?>');
                        window.location = 'category_journal.php';
                    </script>
                    <?php
                }

                if ($dao_category_journal->save($cat_journal_id, $journal_id, $category_id, $description)) {
                    if ($type == "edit") {
                        $message = "Edit relationships successfully!";
                    } else {
                        $message = "Inserted relationships successfully!";
                    }
                    ?>
                    <script type="text/javascript">
                        alert('<?php echo $message; ?>');
                        window.location = 'category_journal.php';
                    </script>
                <?php } else { ?>
                    <script type="text/javascript">
                        alert('Failed!');
                    </script>
                    <?php
                }
            }
        }

        //Edit
        if (!empty($_POST['btnEdit']) && $type = "edit") {
            if (!empty($_POST['ddlJournals']) && !empty($_POST['ddlCategory']) && !empty($_POST['txtDescription'])) {
                $edit_journal = $_POST['ddlJournals'];
                $edit_category = $_POST['ddlCategory'];
                $edit_description = $_POST['txtDescription'];
                $edit_cat_journal_id = $_POST['cat_journal_id'];

                if ($dao_category_journal->edit($edit_cat_journal_id, $edit_journal, $edit_category, $edit_description)) {
                    ?>
                    <script type="text/javascript">
                        alert('Edit successfully!');
                        window.location = 'category_journal.php';
                    </script>
                    <?php
                } else {
                    ?>
                    <script type="text/javascript">
                        alert('Edit failed!');
                        window.location = 'category_journal.php';
                    </script>
                    <?php
                }
            } else {
                ?>
                <script type="text/javascript">
                    alert('The information to edit is missing!');
                    window.location = 'category_journal.php';
                </script>
                <?php
            }
        }

        //Get Edit Item
        if (isset($type) && $type == 'edit') {
            $edit_item = $dao_category_journal->get_by_id($cat_journal_id);
        }


        //Delete
        if (!empty($_POST["delete_cat_journal_id"])) {

            $delete_cat_journal_id = $_POST["delete_cat_journal_id"];

            if ($dao_category_journal->delete($delete_cat_journal_id)) {
                ?>
                <script type="text/javascript">
                    alert('Delete relationships successfully!');
                    window.location = 'category_journal.php';
                </script> 
            <?php } else { ?>
                <script type="text/javascript">
                    alert('Failed to delete relationships!');
                </script> 
                <?php
            }
        }
        ?>
        <div>
            <div class="content-box-outer">
                <div class="content-box-right">
                </div>
                <div class="content-box-left">
                    <form action="category_journal.php" method="POST" id="form_delete" >
                        <input type="hidden" name="delete_cat_journal_id" value="<?php echo $delete_cat_journal_id; ?>" />
                    </form>
                    <form action="category_journal.php" method="POST" id="form_category_journal" onsubmit="call()" >
                        <input type="hidden" name="a_type" value="insert" />
                        <h1>Journals & Categories</h1>
                        <input type="hidden" name="cat_journal_id" value="<?php
                        if (!empty($edit_item))
                            echo $edit_item['cat_journal_id'];
                        else
                            echo 0;
                        ?>" /> 

                        <div class="TitleArea"><b>Select Journal: </b></div>
                        <div class="textBoxArea">
                            <select name="ddlJournals" id="ddlJournals" style="width:300px;">
                                <?php
                                $list = $journal_access->get_all();

                                foreach ($list as $i => $item) :
                                    ?>
                                    <option value="<?php echo $item['JournalID'] ?>"
                                    <?php
                                    if (!empty($edit_item)) {
                                        if ($item['JournalID'] == $edit_item['JournalID']):
                                            echo 'selected';
                                        endif;
                                    }
                                    ?>
                                            > <?php echo $item['Title'] ?> 
                                    </option>
                                    <?php
                                endforeach;

                                $journal_list = $list;
                                $journal_id = 0;

                                if (!empty($edit_item['JournalID'])) {
                                    $journal_id = $edit_item['JournalID'];
                                } else if (!empty($journal_list) && sizeof($journal_list)) {
                                    $journal_id = $journal_list[0]['JournalID'];
                                }
                                ?>
                            </select>
                        </div>
                        <div class="TitleArea"><b>Select Category: </b></div>
                        <div class="textBoxArea">
                            <select name="ddlCategory" id="ddlCategory" style="width:300px;">
                                <?php
                                $list_category = $category_access->get_all();

                                foreach ($list_category as $item):
                                    ?>
                                    <option value="<?php echo $item['CategoryID'] ?>"
                                    <?php
                                    if (!empty($edit_item)) :
                                        if ($edit_item['CategoryID'] == $item['CategoryID']):
                                            echo 'selected';
                                        endif;
                                    endif;
                                    ?>
                                            >
                                                <?php echo $item['Title'] ?>
                                    </option>}
                                    option
                                    <?php
                                endforeach;
                                ?>
                            </select>
                        </div>
                        <br>
                        <div class="TitleArea"><b>Description: </b></div>
                        <div style="margin-top: 100px;">
                            <input type="hidden" name="hiddenMsg" value="">
                            <textarea class="ckeditor" id="txtDescription" name="txtDescription">
                                <?php
                                if (!empty($edit_item['description'])) {
                                    echo $edit_item['description'];
                                }
                                ?>
                            </textarea>
                        </div>
                        <div class="textBoxArea" style="margin-bottom: 30px;">
                            <input type="submit" name="btnSave" value="Insert" id="btnSave" class="buttonDesign">
                            <input type="submit" name="btnEdit" value="Edit" id="btnSave" class="buttonDesign">
                            <input type="submit" name="btnClear" value="Clear" id="btnClear" class="buttonDesign">
                        </div>
                    </form>
                    <br clear="all">
                    <h1>Manage Journals & Categories Relationships</h1>
                    <div>
                        <table cellspacing="0" cellpadding="4" rules="cols" id="cphAdmin_gvJournalSubDetail" style="color:Black;background-color:White;border-color:#DEDFDE;border-width:1px;border-style:None;width:988px;border-collapse:collapse;">
                            <tbody>
                                <tr style="color:White;background-color:#6B696B;font-weight:bold;">
                                    <th scope="col">Journal Title</th>
                                    <th scope="col">Category Title</th>
                                    <th scope="col">Description</th>
                                    <th scope="col">Actions</th>
                                </tr>
                                <?php
                                $current_page = 1;
                                $page_size = 10;
                                if (!empty($_GET['p'])) {
                                    $current_page = $_GET['p'];
                                }

                                $list_total = $dao_category_journal->get_all_order_by_journal_id();
                                $total_page = $lib_pager->get_total_page($list_total, $page_size);
                                $current_page_list = $lib_pager->get_current_page_list($page_size, $current_page, $list_total);

                                $count = 0;
                                foreach ($current_page_list as $i => $item) :
                                    if ($count % 2 == 0) {
                                        ?>
                                        <tr style="background-color:#F7F7DE;">
                                        <?php } else { ?>
                                        <tr style="background-color:#FFFFFF;">
                                        <?php } ?>
                                        <td><?php echo $item['JournalID']; ?></td>
                                        <td style="width:200px;"><?php echo $item['CategoryID']; ?></td>
                                        <td><?php echo substr(strip_tags($item['description']), 0, 200); ?></td>
                                        <td align="center">
                                            <a href="category_journal.php?t=e&cj_id=<?php echo $item['cat_journal_id'] ?>">
                                                <img src="<?php echo LINK_ROOT . DIR_ADMIN_LAYOUT_IMAGE; ?>/edit.png" style="width:15px; outline: none;" />
                                            </a>
                                            <input type="image" 
                                                   name="ImageButtonDelete<?php echo $count; ?>" 
                                                   id="ImageButtonDelete<?php echo $count; ?>" 
                                                   title="Delete" 
                                                   src="<?php echo LINK_ROOT . DIR_ADMIN_LAYOUT_IMAGE; ?>/delete.png" 
                                                   onclick="return my_confirm(<?php echo $item['cat_journal_id'] ?>);" style="width:15px;">
                                        </td>
                                    </tr>
                                    <?php
                                    $count++;
                                endforeach;
                                ?>
                                <tr align="right" style="color:Black;background-color:#F7F7DE;">
                                    <td colspan="4">
                                        <table>
                                            <tbody>
                                                <tr align="right" style="color:Black;background-color:#F7F7DE;">
                                                    <td colspan="4">
                                                        <table>
                                                            <tbody>
                                                                <tr>
                                                                    <?php for ($a = 1; $a <= $total_page; $a++) { ?>
                                                                        <td>
                                                                            <span>
                                                                                <a href="category_journal.php?p=<?php echo $a; ?>" <?php if ($current_page != $a) echo 'style="color:Black;"'; ?>><?php echo $a; ?></a>
                                                                            </span>
                                                                        </td>
                                                                    <?php } ?>
                                                                </tr>
                                                            </tbody>
                                                        </table>
                                                    </td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>

                </div>

                <div class="MessageArea">
                    <span id="cphAdmin_lblMsg"></span>
                </div>
            </div>

        </div>

        <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'footer.php';
        ?>
    </body>
</html>
